package com.xjgc.msibase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjgc.msibase.entity.MsiExoSystem;
import com.xjgc.msibase.entity.MsiExoSystemServer;
import com.xjgc.msibase.mapper.MsiExoSystemMapper;
import com.xjgc.msibase.mapper.MsiExoSystemServerMapper;
import com.xjgc.msibase.service.IMsiExoSystemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 外围系统注册
 * @Author: 小逗号
 * @Date: 2023-10-30
 * @Version: V1.0
 */
@Slf4j
@Service
public class MsiExoSystemServiceImpl
        extends ServiceImpl<MsiExoSystemMapper, MsiExoSystem>
        implements IMsiExoSystemService {

    @Resource
    private MsiExoSystemServerMapper msiExoSystemServerMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMain(MsiExoSystem msiExoSystem, List<MsiExoSystemServer> msiExoSystemServerList) {
        baseMapper.insert(msiExoSystem);
        if (msiExoSystemServerList != null && msiExoSystemServerList.size() > 0) {
            for (MsiExoSystemServer entity : msiExoSystemServerList) {
                //外键设置
                entity.setExoSystemId(msiExoSystem.getId());
                msiExoSystemServerMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMain(MsiExoSystem msiExoSystem, List<MsiExoSystemServer> msiExoSystemServerList) {
        baseMapper.updateById(msiExoSystem);

        //1.先删除子表数据
//        msiExoSystemServerMapper.deleteByMainId(msiExoSystem.getId());
        msiExoSystemServerMapper.delete(new LambdaQueryWrapper<MsiExoSystemServer>().eq(MsiExoSystemServer::getExoSystemId, msiExoSystem.getId()));
        //2.子表数据重新插入
        if (msiExoSystemServerList != null && msiExoSystemServerList.size() > 0) {
            for (MsiExoSystemServer entity : msiExoSystemServerList) {
                //外键设置
                entity.setExoSystemId(msiExoSystem.getId());
                entity.setExoSystemCode(msiExoSystem.getExoSystemCode());
                msiExoSystemServerMapper.insert(entity);
            }
        }
    }


}
